<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class UserSessionModel extends Model
{

	//更新在线状态
    public function updateSession($userID)
    {
        $sessionID = cookie('session_id');

    	if (empty($sessionID))
    	{
    	    $time = time();
    	    $ip = get_client_ip();
    	    $sessionID = kg_crc32($time . $ip . kg_rand_string());
    		$onlineHoldTime = intval(get_site_config('online_hold_time'));
    		$onlineHoldTime = $onlineHoldTime ? 60 * $onlineHoldTime : 1800;

    		$map = array('last_activity' => array('lt', $time - $onlineHoldTime));
    		$this->where($map)->delete();

    		$User = M('User');
    		$user = !empty($userID) ? $User->find($userID) : array();
    		$data = array(
        		'session_id'    => $sessionID,
        		'user_id'       => !empty($user) ? intval($user['user_id']) : 0,
        		'username'      => !empty($user) ? $user['username'] : L('online_guest'),
        		'user_group'    => !empty($user) ? intval($user['user_group']) : 6,
        		'invisible'     => !empty($user) ? intval($user['invisible']) : 0,
        		'last_activity' => $time,
        		'last_ip'       => $ip,
    		);
            $this->replace($data);
    		if (!empty($user))
    		{
    			$map  = array('user_id' => $userID);
    			$data = array(
    				'last_activity' => $time,
    				'last_ip' => $ip,
    			);
    			$User->where($map)->save($data);
    		}
    		cookie('session_id', $sessionID, $onlineHoldTime);
    	}
    }


    //清除在线状态
    public function unsetSession($userID)
    {
        $where = array('user_id' => $userID);
        $this->where($where)->delete();
    }

}
//End Class


/* End of file UserSessionModel.class.php */
/* Location: ./App/Lib/Model/UserSessionModel.class.php */